package com.hsx.servlet.user;

import com.hsx.pojo.User;
import com.hsx.service.user.UserServiceImpl;
import com.hsx.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//控制层调用业务层代码
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入了LoginServlet");
//        attribute 更强调的是物体bai本身的du性，
//        zhiparameter是参数的意思，dao如果你知道函zhuan数的话，
//        自shu变量就是parameter，而不能用attribute。
//        物体属本身的颜色是attribute而不是parameter。
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");

        UserServiceImpl userService = new UserServiceImpl();
        User user = userService.login(userCode, userPassword);
        if(user!=null){
            req.getSession().setAttribute(Constants.USER_SESSION,user);
           resp.sendRedirect("jsp/frame.jsp");
        }else{
            req.getSession().setAttribute("error","用户名或者密码不正确");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
